//
//  YSCountDownView.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-10-15.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSCountDownView.h"
#import "YSUi.h"


@interface YSCountDownView () {
    
    NSTimer *timer;
    
    NSArray *imagesArray;
    UIImageView *countDownImageView;
    
    NSInteger nIndex;
}

@end

@implementation YSCountDownView

- (id)initWithPoint:(CGPoint)point {
    self = [super initWithFrame:CGRectMake(point.x, point.y, countDownSize, countDownSize)];
    if (self) {
        // Initialization code
        imagesArray = @[@"head_kong",@"head_kong1",@"head_kong2",
                        @"head_kong3",@"head_kong4",@"head_kong5",
                        @"head_kong6",@"head_kong7",@"head_kong8",
                        @"head_kong9",@"head_kong10",@"head_kong11",
                        @"head_kong12",@"head_kong13",@"head_kong14",
                        @"head_kong15",@"head_kong16",@"head_kong17",
                        @"head_kong18",@"head_kong19",@"head_kong20",
                        @"head_kong21",@"head_kong22",@"head_kong23",
                        @"head_kong24",@"head_kong25",@"head_kong26",
                        @"head_kong27", @"head_kong28",@"head_kong29",
                        @"head_kong30"];
        
        countDownImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, countDownSize, countDownSize)];
        countDownImageView.backgroundColor = [UIColor clearColor];

        [self addSubview:countDownImageView];
    }
    return self;
}

- (void)beginCountDown {
    if (timer) {
        timer = nil;
    }
    
    nIndex = 1;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1
                                             target:self
                                           selector:@selector(countDown)
                                           userInfo:nil
                                            repeats:YES];
}

- (void)countDown {
    countDownImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:nIndex - 1]];
    
    if (nIndex < imagesArray.count) {
        nIndex ++;
    } else {
        [timer invalidate];
        timer = nil;
        if (_block) {
            _block();
        }
    }
    
}

- (void)stopCountDown {
    [timer invalidate];
    timer = nil;
    
    countDownImageView.image = nil;
}

@end
